package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000211Bo;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000212Bo;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000213Bo;
import com.tianqu.tms.dto.TroubleCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.www.car.actionform.CarStockModifyForm;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class CarStockModifyResultController  extends TmsController {

	
	public void executeModifyEvaluate(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			CarStockModifyForm inputForm = (CarStockModifyForm)uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			Car car = new Car();
			car.setSerial(inputForm.getSerial());
			car.setAttestStatus(inputForm.getAttestStatus());
			car.setInternalScore(inputForm.getInternalScore());
			car.setExternalScore(inputForm.getExternalScore());
			car.setDetectRemark(inputForm.getDetectRemark());
			car.setUpdateUserId(profile.getUniqueId());
			car.updateCarEvaluate(car);
			
			if(inputForm.getOldAttestStatus() != inputForm.getAttestStatus()){//检测状态被修改
				if( (inputForm.getAttestStatus() !=1 && inputForm.getAttestStatus() !=2 ) 
						&&(inputForm.getOldAttestStatus() == 1 || inputForm.getOldAttestStatus() == 2)){//如果车辆检测状态（attestStatus）被修改，并且页面选择的检测结果为非“1：未检测、2：放心车”并且 车辆检测状态（attestStatus）原来值为“1：未检测、2：放心车
					//tms_trouble_car_info
					TroubleCarInfoDto dto = new TroubleCarInfoDto();
					dto.setRemark(inputForm.getDetectRemark());
					dto.setTroubleType(inputForm.getAttestStatus());
					dto.setVin(inputForm.getVin());
					car.insertTroubleCarInfo(dto);
				}else if((inputForm.getAttestStatus() !=1 && inputForm.getAttestStatus() !=2 ) 
						&&(inputForm.getOldAttestStatus() != 1 && inputForm.getOldAttestStatus() != 2)){//如果车辆检测状态（attestStatus）被修改，并且页面选择的检测结果为非“1：未检测、2：放心车”并且 车辆检测状态（attestStatus）原来值非“1：未检测、2：放心车”，那么更新问题车辆信息表
					TroubleCarInfoDto dto = new TroubleCarInfoDto();
					dto.setRemark(inputForm.getDetectRemark());
					dto.setTroubleType(inputForm.getAttestStatus());
					dto.setVin(inputForm.getVin());
					car.updateTroubleCarInfo(dto);
				}else if((inputForm.getAttestStatus() ==1 || inputForm.getAttestStatus() ==2 ) 
						&&(inputForm.getOldAttestStatus() != 1 && inputForm.getOldAttestStatus() != 2)){//如果车辆检测状态（attestStatus）被修改，并且页面选择的检测结果为非“1：未检测、2：放心车”并且 车辆检测状态（attestStatus）原来值非“1：未检测、2：放心车”，那么更新问题车辆信息表
					TroubleCarInfoDto dto = new TroubleCarInfoDto();
					dto.setVin(inputForm.getVin());
					car.deleteTroubleCarInfo(dto);
				}
				//mq
				MobileMessage message = new MobileMessage();
				message.putValue("Serial", car.getSerial());
				message.setMessage("【TMS】新增入库（库存存在情况的更新保存）【车代办】同步,Serial为#Serial#");
				TransferDataSyn000213Bo api = new TransferDataSyn000213Bo(); 
				api.setSerial(car.getSerial());
				api.setAttestStatus(car.getAttestStatus());
				if(StringUtil.isNotBlank(car.getInternalScore())) api.setInternalScore(Integer.parseInt(car.getInternalScore()));
				if(StringUtil.isNotBlank(car.getExternalScore())) api.setExternalScore(Integer.parseInt(car.getExternalScore())); 
				api.setDetectRemark(car.getDetectRemark());
				api.setMarketNo(TmsConstants.MARKET_NO);
				PullingExecutor.getInstance().submit(api);
			}
			//
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
	
	public void executeModifyPic(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
			viewhelper = (CarStockModifyViewhelper)uc.getAttribute("viewhelp");
			CarStockModifyForm inputForm = (CarStockModifyForm)uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			Car car = new Car();
			car.setSerial(viewhelper.getSerial());
			/* hrz
			 * car.setSerial(inputForm.getSerial());
			car.setPhoto_1(inputForm.getImg1());
			car.setPhoto_2(inputForm.getImg2());
			car.setPhoto_3(inputForm.getImg3());
			car.setPhoto_4(inputForm.getImg4());
			car.setPhoto_5(inputForm.getImg5());
			car.setPhoto_6(inputForm.getImg6());
			car.setPhoto_7(inputForm.getImg7());
			car.setPhoto_8(inputForm.getImg8());*/
			car.setPhoto_1(viewhelper.getImg1());
			car.setPhoto_2(viewhelper.getImg2());
			car.setPhoto_3(viewhelper.getImg3());
			car.setPhoto_4(viewhelper.getImg4());
			car.setPhoto_5(viewhelper.getImg5());
			car.setPhoto_6(viewhelper.getImg6());
			car.setPhoto_7(viewhelper.getImg7());
			car.setPhoto_8(viewhelper.getImg8());
			car.setUpdateUserId(profile.getUniqueId());
			car.updateCarPic(car);
			controller.commit();
			
			MobileMessage message = new MobileMessage();
			message.putValue("Serial", car.getSerial());
			message.setMessage("【TMS】新增入库（库存存在情况的更新保存）【车代办】同步,Serial为#Serial#");
			TransferDataSyn000212Bo api = new TransferDataSyn000212Bo(); 
			api.setSerial(car.getSerial());
			api.setPhoto1(car.getPhoto_1());
			api.setPhoto2(car.getPhoto_2());
			api.setPhoto3(car.getPhoto_3());
			api.setPhoto4(car.getPhoto_4());
			api.setPhoto5(car.getPhoto_5());
			api.setPhoto6(car.getPhoto_6());
			api.setPhoto7(car.getPhoto_7());
			api.setPhoto8(car.getPhoto_8());
			api.setMarketNo(TmsConstants.MARKET_NO);
			PullingExecutor.getInstance().submit(api);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
	
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			
			CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
			viewhelper = (CarStockModifyViewhelper)uc.getAttribute("viewhelp");
			CarStockModifyForm inputForm = (CarStockModifyForm)uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			Car car = new Car();
			Car car1 = new Car();
			car1 = car1.findCarBySerial(viewhelper.getSerial());
			car.setLicensePlate(viewhelper.getLicensePlate());
			car.setCarScore(viewhelper.getCarScore());
			car.setVin(viewhelper.getVin());
			if (!StringUtil.isNullOrBlank(viewhelper.getMileage()))
				car.setMileage(Integer.parseInt(StringUtil.removeDH(viewhelper.getMileage())));
			if (!StringUtil.isNullOrBlank(viewhelper.getPrice()))
				car.setPrice(Integer.parseInt(StringUtil.removeDH(viewhelper.getPrice())));
			if (!StringUtil.isNullOrBlank(viewhelper.getColor()))
				car.setColor(Integer.parseInt(viewhelper.getColor()));
			car.setRegDate(viewhelper.getRegDate());
			car.setJqx(viewhelper.getJqx());
			car.setYearCarefulPeriod(viewhelper.getYearCarefulPeriod());
			car.setCompanyContactPerson(viewhelper.getCompanyContactPerson());
			car.setCompanyContactPhone(viewhelper.getCompanyContactPhone());
			car.setRemark(viewhelper.getRemark());
			//
			if(StringUtil.isNotBlank(viewhelper.getCarStyleId())){
				CarStyle carStyle = CarStyle.getInstance();
				String styleName = carStyle.getCarStyleRNameById(viewhelper.getCarStyleId());
				car.setCarName(styleName);
			}
			car.setPhoto_1(viewhelper.getPhoto_1());
			car.setPhoto_2(viewhelper.getPhoto_2());
			car.setPhoto_3(viewhelper.getPhoto_3());
			car.setPhoto_4(viewhelper.getPhoto_4());
			car.setPhoto_5(viewhelper.getPhoto_5());
			car.setPhoto_6(viewhelper.getPhoto_6());
			car.setPhoto_7(viewhelper.getPhoto_7());
			car.setPhoto_8(viewhelper.getPhoto_8());
			car.setVendorId(viewhelper.getVendorId());
			car.setBrandId(viewhelper.getBrandId());
			car.setCarStyleId(viewhelper.getCarStyleId());
			car.setUpdateUserId(profile.getUniqueId());
			car.setSerial(viewhelper.getSerial());
			car.setIsShelve(viewhelper.getIsShelve());
			car.setIsMatch(viewhelper.getIsMatch());
			car.setAttestStatus(viewhelper.getAttestStatus());
			if(inputForm.getFromType() == 1){
				car.setCatClass(2);
			}else if(inputForm.getFromType() == 2){
				car.setCatClass(1);
			}
			if(car1.getEntryStatus() == 2 || car1.getEntryStatus() == 1){
				car.setEntryStatus(4);
			}else {
				car.setEntryStatus(car1.getEntryStatus());
			}
			
			StringBuffer ConfigKeysList = new StringBuffer();
			if (viewhelper.getLdpz1()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz2()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz3()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz4()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz5()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz6()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz7()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz8()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz9()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz10()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz11()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz12()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz13()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz14()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz15()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz16()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz17()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz18()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz19()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz20()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz21()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz22()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz23()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz24()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz25()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz26()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz27()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz28()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz29()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz30()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz31()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz32()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz33()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz34()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz35()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz36()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz37()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz38()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz39()) {ConfigKeysList.append("1,");}else{ConfigKeysList.append("2,");}
			if (viewhelper.getLdpz40()) {ConfigKeysList.append("1");}else{ConfigKeysList.append("2");}

			car.setConfigKey(ConfigKeysList.toString());
			car.updateMarketCarInfo(car);
			controller.commit();
			
			//
			car = car.findCarBySerial(car.getSerial());
			MobileMessage message = new MobileMessage();
			message.putValue("Serial", car.getSerial());
			message.setMessage("【TMS】新增入库（库存存在情况的更新保存）【车代办】同步,Serial为#Serial#");
			TransferDataSyn000211Bo api = new TransferDataSyn000211Bo();
			api.setMobileMessage(message);
			api.setSerial(car.getSerial());
			api.setLicensePlate(car.getLicensePlate());
			api.setVin(car.getVin());
			api.setMemberId(StringUtil.getString(car.getMemberId()));
			api.setCatClass(car.getCatClass());
			api.setMileage(car.getMileage());
			api.setBrandId(car.getBrandId());
			api.setVendorId(car.getVendorId());
			api.setCarStyleId(car.getCarStyleId());
			api.setColor(car.getColor());
			api.setPrice(car.getPrice());
			api.setRegDate(car.getRegDate());
			api.setJqx(car.getJqx());
			api.setYearCarefulPeriod(car.getYearCarefulPeriod());
			api.setCompanyContactPerson(String.valueOf(car.getCompanyContactPerson()));
			api.setCompanyContactPhone(car.getCompanyContactPhone());
			api.setRemark(car.getRemark());
			api.setIsMatch(String.valueOf(car.getIsMatch()));
			api.setRegisterStatus(car.getRegisterStatus());
			api.setCarName(car.getCarName());
			api.setIsShelve(car.getIsShelve());
			api.setMarketNo(TmsConstants.MARKET_NO);
			api.setConfigKey(car.getConfigKey());
			api.setEntryStatus(car.getEntryStatus());
			api.setBarCodeNum(car.getBarCodeNum());
			PullingExecutor.getInstance().submit(api);
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}
